﻿Imports System.Runtime.InteropServices
Public Class skymap

    Private Sub skymap_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

    End Sub
    Public Declare Function WNetAddConnection2 Lib "mpr.dll" Alias "WNetAddConnection2A" _
(ByRef lpNetResource As NETRESOURCE, ByVal lpPassword As String, _
ByVal lpUserName As String, ByVal dwFlags As Integer) As Integer

    Public Declare Function WNetCancelConnection2 Lib "mpr" Alias "WNetCancelConnection2A" _
   (ByVal lpName As String, ByVal dwFlags As Integer, ByVal fForce As Integer) As Integer

    <StructLayout(LayoutKind.Sequential)> _
    Public Structure NETRESOURCE
        Public dwScope As Integer
        Public dwType As Integer
        Public dwDisplayType As Integer
        Public dwUsage As Integer
        Public lpLocalName As String
        Public lpRemoteName As String
        Public lpComment As String
        Public lpProvider As String
    End Structure

    Public Const ForceDisconnect As Integer = 1
    Public Const RESOURCETYPE_DISK As Long = &H1

    Public Function MapDrive(ByVal DriveLetter As String) As Boolean

        Dim nr As NETRESOURCE
        Dim strUsername As String
        Dim strPassword As String

        nr = New NETRESOURCE
        nr.lpRemoteName = txtlocation.Text
        nr.lpLocalName = DriveLetter & ":"
        strUsername = txtUsername.Text '(add parameters to pass this if necessary)
        strPassword = txtPassword.Text '(add parameters to pass this if necessary)
        nr.dwType = RESOURCETYPE_DISK

        Dim result As Integer
        result = WNetAddConnection2(nr, strPassword, strUsername, 0)

        'MsgBox(strUsername & strPassword & nr.lpRemoteName & nr.lpLocalName)

        If result = 0 Then
            MsgBox("Drive Mapped - You can now access Skydrive from the My Computer window.")
            Return True
        Else
            MsgBox("Mapping Failed: Either you entered in the wrong url, or M$ changed things again.  " & vbCrLf & Err.Description & vbCrLf & Err.Source, vbOKOnly - vbExclamation, )
            Return False
        End If
    End Function

    Public Function UnMapDrive(ByVal DriveLetter As String) As Boolean
        Dim rc As Integer
        rc = WNetCancelConnection2(DriveLetter & ":", 0, ForceDisconnect)

        If rc = 0 Then
            MsgBox("Drive Unmapped")
            Return True
        Else
            MsgBox("Failed:" & vbCrLf & Err.Description & vbCrLf & Err.Source, vbOKOnly - vbExclamation, )
            Return False
        End If

    End Function

    Private Sub cmdMap_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdmap.Click
        txtlocation.Text = txtlocation.Text.Remove(0, 51) ' IF SKYMAP IS NOT WORKING, THIS IS WHERE THE ERROR IS. M$ changes the url all the freakin time. It needs the code after "cid=" in the URL. The '0, 51' is the characters its removing from this to get the id: ( https://onedrive.live.com/?gologin=1&mkt=en-US#cid=acc342d72f7b258d)
        Dim text As String
        text = txtlocation.Text
        Dim adloc As String
        adloc = "\\d.docs.live.net@SSL\DavWWWRoot\" + text
        txtlocation.Text = adloc
        ' Dim Location As String
        ' Dim Username As String
        ' Dim Password As String
        ' Dim Domain As String
        ' Dim Letter As String
        '
        'Location = txtlocation.Text
        ' Username = txtUsername.Text
        ' Password = txtPassword.Text
        'Domain = txtDomain.Text
        '  Letter = "S:"
        '
        ' System.Diagnostics.Process.Start("net.exe use " & Letter & " " & Location & " /user:" & Username & " " & Password)

        MapDrive(letter.Text)
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        UnMapDrive(letter.Text)
    End Sub
End Class